package com.hzvdvmc.autosale.setting.ui;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.base.ActionbarActivity;
import com.hzvdvmc.autosale.constant.SpKey;
import com.hzvdvmc.autosale.constant.TimeConstant;
import com.hzvdvmc.autosale.sale.ui.InputDialog;
import com.hzvdvmc.autosale.util.SPUtils;
import com.hzvdvmc.autosale.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TimeSetActivity extends ActionbarActivity {

    @BindView(R.id.tv_wait_time)
    TextView tvWaitTime;
    @BindView(R.id.layout_wait)
    LinearLayout layoutWait;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.layout_pay)
    LinearLayout layoutPay;
    @BindView(R.id.tv_adver_time)
    TextView tvAdverTime;
    @BindView(R.id.layout_adver)
    LinearLayout layoutAdver;
    @BindView(R.id.tv_label_wait)
    TextView tvLabelWait;
    @BindView(R.id.tv_label_pay)
    TextView tvLabelPay;
    @BindView(R.id.tv_label_adver)
    TextView tvLabelAdver;

    @BindView(R.id.iv_back)
    ImageView ivBack;
    private InputDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_set);
        ButterKnife.bind(this);

        initData();

        setRightFuncBtn("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtils.getInstance().put(SpKey.TIME_ADVER, tvAdverTime.getText().toString());
                SPUtils.getInstance().put(SpKey.TIME_PAY, tvPayTime.getText().toString());
                SPUtils.getInstance().put(SpKey.TIME_WAIT, tvWaitTime.getText().toString());
                ToastUtils.showShort("保存成功 ");
            }
        });
    }

    private void initData() {
        tvAdverTime.setText(SPUtils.getInstance().getString(SpKey.TIME_ADVER));
        tvWaitTime.setText(SPUtils.getInstance().getString(SpKey.TIME_WAIT));
        tvPayTime.setText(SPUtils.getInstance().getString(SpKey.TIME_PAY));
    }

    @OnClick({R.id.layout_wait, R.id.layout_pay, R.id.layout_adver})
    public void onClick(View view) {
        showInputDialog(view.getId());
    }
    @OnClick(R.id.iv_back)
    public void onClickView(){
        finish();
    }

    public void showInputDialog(final int viewId) {
        String title = "";
        String value = "";
        switch (viewId) {
            case R.id.layout_wait:
                title = tvLabelWait.getText().toString();
                value = tvWaitTime.getText().toString();
                break;
            case R.id.layout_pay:
                title = tvLabelPay.getText().toString();
                value = tvPayTime.getText().toString();
                break;
            case R.id.layout_adver:
                title = tvLabelAdver.getText().toString();
                value = tvAdverTime.getText().toString();
                break;
        }
        if (dialog == null) {
            dialog = new InputDialog(this, R.style.MyDialog, InputType.TYPE_CLASS_NUMBER);
            dialog.setEditLength(3);
        }

        dialog.setOkOnClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputContent = dialog.getInputContent();
                if (TextUtils.isEmpty(inputContent)) {
                    ToastUtils.showShort("输入内容不能为空");
                    return;
                }
                switch (viewId) {
                    case R.id.layout_wait:
                        tvWaitTime.setText(inputContent);
                        break;
                    case R.id.layout_pay:
                        tvPayTime.setText(inputContent);
                        break;
                    case R.id.layout_adver:
                        tvAdverTime.setText(inputContent);
                        break;
                }

            }
        });

        dialog.setCurrentValue(value);
        dialog.setTitle(title);
        dialog.show();
    }

}
